$(function () {
    var clientPoolDatagrid = $("#clientPool_datagrid");
    var clientForm = $("#client_form");
    var clientDialog = $("#client_dialog");
    var dispatchForm = $("#dispatch_form");
    var dispatchDialog = $("#dispatch_dialog");
    clientPoolDatagrid.datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        url: "/client/listNoSaleman",
        pagination: true,
        singleSelect: true,
        rownumbers: true,
        toolbar: "#clientPool_toolbar",
        columns: [[
            {field: 'name', title: '姓名', width: 70, align: 'center'},
            {field: 'traceNum', title: '跟进次数', width: 70, align: 'center'},
            {field: 'lastTraceTime', title: '最后跟进时间', width: 70, align: 'center'},
            {field: 'qq', title: 'QQ', width: 70, align: 'center'},
            {field: 'tel', title: '电话', width: 70, align: 'center'},
            {field: 'school', title: '学校', width: 70, align: 'center'},
            {field: 'client.intentionDegree', title: '意向程度', formatter:formatterIntentionDegree,width: 70, align: 'center'},
            {field: 'client.campus', title: '意向校区', formatter:formatterCampus,width: 70, align: 'center'},
            {field: 'client.clz', title: '意向班级', formatter:formatterClz,width: 70, align: 'center'},
            {field: 'remark', title: '备注', width: 70, align: 'center'}
        ]]
    });
    
    var cmdObj = {
        //查看
        'see':function () {
            clientDialog.dialog("setTitle","查看");
            $(":input").prop('disabled','disabled');
            //获得当前行对象
            var selectRow = clientPoolDatagrid.datagrid("getSelected");
            //如果没有行被选中
            if (!selectRow) {
                $.messager.alert("提示","请选中想要查看的一行","error");
                return;
            }
            //将数据回显到表单中
            if (selectRow.eduBackground) {
                selectRow['eduBackground.id']=selectRow.eduBackground.id;
            }
            if (selectRow.bigCustomer){
                selectRow['bigCustomer.id']=selectRow.bigCustomer.id;
            }
            if (selectRow.intentionDegree) {
                selectRow['intentionDegree.id']=selectRow.intentionDegree.id;
            }
            if (selectRow.statue) {
                selectRow['statue.id']=selectRow.statue.id;
            }
            if (selectRow.clz) {
                selectRow['clz.id']=selectRow.clz.id;
            }
            if (selectRow.campus) {
                selectRow['campus.id']=selectRow.campus.id;
            }
            if (selectRow.intentionSubject) {
                selectRow['intentionSubject.id']=selectRow.intentionSubject.id;
            }
            if (selectRow.source) {
                selectRow['source.id']=selectRow.source.id;
            }
            if (selectRow.hasPaid) {
                selectRow.hasPaid = 1;
            } else {
                selectRow.hasPaid = 0;
            }
            clientForm.form("load",selectRow);
            //打开点击对话框
            clientDialog.dialog("setTitle","客户查看");
            clientDialog.dialog();
            clientDialog.dialog("open");
        },//刷新数据列表
        'reload':function () {
            clientPoolDatagrid.datagrid("reload");
        },
        //指派给我
        'joinme':function () {
            //获得当前行对象
            var selectRow = clientPoolDatagrid.datagrid("getSelected");
            //如果没有行被选中
            if (!selectRow) {
                $.messager.alert("提示","请选中想要跟进的对象","error");
                return;
            }
            $.messager.confirm('确认信息', '您确认要跟进该客户?', function (r) {
                if (r) {
                    $.get('/clientpool/joinMe?clientId='+selectRow.id, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '移交成功', 'info', function () {
                                clientPoolDatagrid.datagrid("reload");
                            })
                        } else {
                            $.messager.alert('温馨提示', data.message, 'info');
                        }
                    });
                }
            });
        },
        //打开指派给谁对话框
        'dispatch':function () {
            $(":input").prop('disabled','');
            //先清除表单数据
            dispatchForm.form("clear");
            //获得当前行对象
            var selectRow = clientPoolDatagrid.datagrid("getSelected");
            //如果没有行被选中
            if (!selectRow) {
                $.messager.alert("提示","请选中想要指派的对象","error");
                return;
            }
            //回显客户姓名
            dispatchForm.form("load",selectRow);
            //打开指派对话框
            dispatchDialog.dialog("open");
        },

        //指派给谁
        'dispatchTo':function () {
            //获得当前行对象
            var selectRow = clientPoolDatagrid.datagrid("getSelected");
            //温馨提示 确认要指派吗
            $.messager.confirm("温馨提示","确认要指派吗?",function (yes) {
                if (yes) {
                    //得到被指派人的id
                    var salemanId = dispatchForm.find("[name='saleman.id']").val();
                    //发送ajax请求 指派
                    $.post("/clientpool/dispatch",{'clientId':selectRow.id,'salemanId':salemanId},function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示","指派成功","info",function () {
                                //刷新数据列表
                                cmdObj.reload();
                                //关闭对话框
                                cmdObj.cancelDispatch();
                            })
                        }
                    },'json');
                }
            })
        },

        //关闭指派给谁对话框
        'cancelDispatch':function () {
            dispatchDialog.dialog("close");
        }
    };

    //统一给data-cmd绑定点击事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data('cmd');
        cmdObj[cmd]();
    });
})

function formatterIntentionDegree(value,row,index) {
    if (row.intentionDegree) {
        return row.intentionDegree.name;
    }
}

function formatterCampus(value,row,index) {
    if (row.campus) {
        return row.campus.name;
    }
}

function formatterClz(value,row,index) {
    if (row.clz) {
        return row.clz.name;
    }
}
